<template>
  <by-background />

  <view class="padding-bottom-20">
    <by-navigation-bar title="收到评价"/>

    <view class="padding-lr-20">
      <template v-if="list.length">
        <by-list-card
          v-for="(item,index) in list"
          :key="item.uid + index"
          :item="item.technician"
          :user-comment="item.userComment"
          :date="dateFormat(item.createTime,'YYYY年MM月DD日 hh:mm:ss')"
          type="myAssessment"
          @showSafeguard="showSafeguard = true"
          @evaluate="viewComment"
          @showMenu="handleShowMenu(item.userComment,item.paySerialNo)"
        />
        <by-load-footer :has-more="hasMore" text="加载中..." />
      </template>
      <by-nodata v-else />
    </view>
  </view>

  <by-page-dialogs
      v-model:show-safeguard="showSafeguard"
      v-model:show-evaluate="showEvaluate"
      :comment-value="commentValue"
  />

  <by-draw-bottom v-model="showMenu" :list="menuList" @click="handleClick" />
</template>

<script setup lang="ts">
import { getTechnicianComment, deleteCommentTechnician } from '@/common/api'
import { jump, GetList, dateFormat, toast } from '@/common/util'
import type { AnyObject } from '@/common/types'

const showSafeguard = ref(false)
const showEvaluate = ref(false)
const showMenu = ref(false)

/* const menuList = [
  { text: '举报记录' },
  { text: '删除记录' }
]*/

interface IMenuItem{
  text: string
}
const menuList = shallowRef<IMenuItem[]>([])
const setItem = ref<AnyObject>({})
const serialNo = ref('')
function handleShowMenu(item:AnyObject, paySerialNo:string) {
  menuList.value = item && !item?.isDeleteStar ? [
    { text: '举报记录' },
    { text: '删除记录' }
  ] : [
    { text: '删除记录' }
  ]
  setItem.value = item
  serialNo.value = paySerialNo
  showMenu.value = true
}

async function handleClick(e:AnyObject) {
  if (setItem.value && !setItem.value?.isDeleteStar && !e.index) {
    jump(`/pages/suggest/index?type=2&from=1&id=${setItem.value.cno}`)
  } else {
    await deleteCommentTechnician({
      serialNo: serialNo.value
    })
    reload()
    toast('操作成功')
  }
  showMenu.value = false
  //
}

onHide(() => {
  showMenu.value = false
})

/* 查看评价*/
const commentValue = ref(0)
function viewComment(e:number) {
  showEvaluate.value = true
  commentValue.value = e
}

/* 获取列表*/
const hasMore = ref(true)
const list = shallowRef<AnyObject[]>([])
const getList = new GetList(getTechnicianComment, {}, (res:AnyObject) => res.records)
async function reload() {
  list.value = await getList.reload()
  hasMore.value = getList._has_more
}
async function nextPage() {
  hasMore.value = getList._has_more
  list.value.push(...await getList.nextPage())
  triggerRef(list)
}
reload()
onPullDownRefresh(reload)
onReachBottom(nextPage)

/* 监听返回*/
const back = computed(() => showSafeguard.value || showMenu.value)
onBackPress(() => {
  if (back.value) {
    close()
    return true
  }
})
function close() {
  showSafeguard.value = showMenu.value = false
}
</script>

<style scoped lang="scss">

</style>
